﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using AMatysiak.XML;
using System.IO;

namespace AMatysiakTest.XML
{
    [TestClass]
    public class SettingsTest
    {
        public class SettingsReal : Settings<SettingsReal>
        {
            public int Number;
            public string Name;
            public DateTime Date;

            public SettingsReal() : base() { }
            public SettingsReal(string path) : base(path) { }
        }

        private const string path = "settings.xml";

        [TestMethod]
        public void createInstance()
        {
            SettingsReal settings = new SettingsReal(path);
            Assert.IsInstanceOfType(settings, typeof(Settings<SettingsReal>));
        }

        [TestMethod]
        public void saveValues()
        {
            DateTime now = DateTime.Now;

            SettingsReal settings = new SettingsReal(path);
            settings.Number = 17;
            settings.Name = "Adam";
            settings.Date = now;
            bool answer = settings.Save();

            Assert.IsTrue(answer);

            settings = new SettingsReal(path);

            Assert.AreEqual(17, settings.Number);
            Assert.AreEqual("Adam", settings.Name);
            Assert.AreEqual(now, settings.Date);
        }

        [TestMethod]
        public void lazyLoading()
        {
            DateTime now = DateTime.Now;

            SettingsReal settings = new SettingsReal(path);
            settings.Number = 17;
            settings.Name = "Adam";
            settings.Date = now;
            settings.Save();

            settings = new SettingsReal();
            settings.Path = path;
            settings.Load();

            Assert.AreEqual(17, settings.Number);
            Assert.AreEqual("Adam", settings.Name);
            Assert.AreEqual(now, settings.Date);
        }

        [TestMethod]
        public void testEvents()
        {
            bool state = false;

            SettingsReal settings = new SettingsReal();
            settings.SettingsEvent += delegate(Settings<SettingsReal> sets, SettingsEventArgs sea)
            {
                state = true;
            };
            settings.Load();

            Assert.IsTrue(state);
        }

        [ClassCleanup]
        public static void deleteSettings()
        {
            File.Delete(path);
        }
    }
}
